const { Transform } = require('stream');
const utils = require('../../utils');

module.exports = function (start, count) {
  const stream = new Transform({ objectMode: true });
  let i = 0;
  count === undefined && (count = NaN);
  stream._transform = async function (chunk, encoding, callback) {
    utils.debug('sliceTransform [chunk]:', chunk);
    if (i < start) {
      i++;
      callback();
    } else if (i >= start + count) {
      stream.push(null);
      callback();
    } else {
      i++;
      stream.push(chunk);
      callback();
    }
  };

  stream.on('finish', function () {
    utils.debug('sliceTransform [finish]');
  });

  return stream;
};